#! /usr/bin/env perl
# -*-Mode: perl;-*-

# make sure we execute perl
#eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
#& eval 'exec perl -S $0 $argv:q'
#if 0;

## $Id$
## * BeginRiceCopyright *****************************************************
## 
## Copyright ((c)) 2002-2019, Rice University 
## All rights reserved.
## 
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
## 
## * Redistributions of source code must retain the above copyright
##   notice, this list of conditions and the following disclaimer.
## 
## * Redistributions in binary form must reproduce the above copyright
##   notice, this list of conditions and the following disclaimer in the
##   documentation and/or other materials provided with the distribution.
## 
## * Neither the name of Rice University (RICE) nor the names of its
##   contributors may be used to endorse or promote products derived from
##   this software without specific prior written permission.
## 
## This software is provided by RICE and contributors "as is" and any
## express or implied warranties, including, but not limited to, the
## implied warranties of merchantability and fitness for a particular
## purpose are disclaimed. In no event shall RICE or contributors be
## liable for any direct, indirect, incidental, special, exemplary, or
## consequential damages (including, but not limited to, procurement of
## substitute goods or services; loss of use, data, or profits; or
## business interruption) however caused and on any theory of liability,
## whether in contract, strict liability, or tort (including negligence
## or otherwise) arising in any way out of the use of this software, even
## if advised of the possibility of such damage. 
## 
## ******************************************************* EndRiceCopyright *

#############################################################################
##
## $Source$ 
##
##   Nathan Tallent.
##
#############################################################################

use strict;
use warnings;

use IO::File;
use File::Spec;
use FindBin qw($Script $RealBin);

use lib "$RealBin/../share/hpctoolkit";
use HPCToolkitVersionInfo;

STDOUT->autoflush(1); 

#############################################################################

my $the_program = $Script;
my $the_usage = 
"usage: ${the_program} <xml-data-file>

Given a list of xml data files (pxml/psxml), list directories and
files that cannot be found on the current platform.
\n";

#############################################################################
## main/driver
#############################################################################

# ----------------------------------------------------------
# Parse the command line
# ----------------------------------------------------------

## Make sure called with minimum number of arguments
my $numArgs = scalar(@ARGV);
if ($numArgs < 1) { die $the_usage; }

my $pxmlfiles = [ ];

my $fileidx = 0;

for ( ; $fileidx < $numArgs; $fileidx++) {
    my $file = $ARGV[$fileidx]; 
    if (! (-r $file)) {
	print STDERR "*** Cannot read file '$file' ***\n"; 
	printUsageAndExit($0);
    } 
    
    push @$pxmlfiles, $file;
}

# ----------------------------------------------------------
# 
# ----------------------------------------------------------

hpcmissing($pxmlfiles);
exit(0);

#############################################################################
## Subroutines
#############################################################################

# printUsageAndExit
sub printUsageAndExit 
{
    my ($command) = @_; # not used now
    print STDOUT $the_usage;
    exit(-1);
}

# printVersionAndExit
sub printVersionAndExit 
{
    my ($command) = @_; # not used now
    print STDERR "${the_program}: ", $HPCToolkitVersionInfo::info, "\n";
    exit(-1);
}


#############################################################################
## hpcmissing
#############################################################################

# hpcmissing: Given a list of pxml/psxml files, find directories and
#   files that cannot be found on the current platform.
# returns: 
# effect: 
# assumes: 
sub hpcmissing
{
    my($fileListRef) = @_;
  
    # ----------------------------------------------------------
    # For each of the xml data files files: 
    # ----------------------------------------------------------
    my $fileidx = 0;
    for ( ; $fileidx < scalar(@$fileListRef); $fileidx++) {
	hpcmissing_sub($fileListRef->[$fileidx]);
    }

    return 0;
}


# hpcmissing_sub: Given a data file, search for missing directories or files.
# Returns 0 on success.
sub hpcmissing_sub
{
    my($pxmlfile) = @_;
    my $line = undef;

    my %processed;

    # Open/prepare pxmlfile
    my $fhndl = new IO::File;

    if (! $fhndl->open("< $pxmlfile")) { 
	print STDERR "Error! Can't open input file '$pxmlfile'\n"; 
	return 0; 
    }
    
    # For every File node
    my $pathRE = '<F n=\"([^"]*)\"';
    my $pathUNKNOWN = "&lt;unknown&gt;"; # <unknown>
    
    while ($line = <$fhndl>) {
	my $path;
	if ( ($path) = ($line =~ /$pathRE/) ) {
	    #print STDERR "--> $path\n";
	    
	    # Do not report the '<unknown>' file
	    next if ($path eq $pathUNKNOWN);

	    my ($vol,$dir,$file) = File::Spec->splitpath($path);

	    # if we have already processed this dir, skip both
	    # additional missing dir and file reports
	    next if ($processed{$dir});

	    # if this dir does not exist, report
	    if (! -d $dir) {
		print STDOUT "Missing directory '$dir'\n";
		$processed{$dir} = 1;
	    } 
	    # if we have not processed this file and it does not exist, report
	    elsif (! $processed{$path} && ! -f $path) {
		print STDOUT "Missing file '$path'\n";
		$processed{$path} = 1;
	    }
	    
	}
    }
    
    # Cleanup
    $fhndl->close();

    return 0;
}
